<template>
  <div>
    <van-nav-bar title="用户登录" />
    <UserForm :user="user" @submint="onSubmit">
      <template #operation>
        <van-button block color="#eb6221" native-type="submit">登录</van-button>
      </template>
      <template #link>
        <router-link to="/register">注册账号</router-link>
      </template>
    </UserForm>
  </div>
</template>

<script>
import UserForm from "../components/UserForm.vue";
import { login } from "../api/user";
import { saveToken } from "../utils/token";
export default {
  components: { UserForm },
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    async onSubmit() {
      // 1、调用登录接口
      const res = await login(this.user);
      // 2、存储token
      saveToken(res.data.data.token);
      // 3、登录成功提示
      this.$toast.success("登录成功");
      // 4 、登录成功，跳转首页
      this.$router.push("/");
    },
  },
};
</script>

<style></style>
